package com.wzy.vx.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wzy.vx.DTO.OrderDTO;
import com.wzy.vx.dataobject.OrderDetail;
import com.wzy.vx.enums.ResuletEnum;
import com.wzy.vx.exception.SellException;
import com.wzy.vx.form.Orderform;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: vx
 * @Package: com.wzy.vx.controller
 * @ClassName: Orderform11OrderDTOConverter
 * @Author: Administrator
 * @Description:
 * @Date: 2019/3/14 9:38
 * @Version: 1.0
 */
public class OrderForm11OrderDTOConverter {

    private static final Logger log= LoggerFactory.getLogger(OrderForm11OrderDTOConverter.class);

    public static OrderDTO convert(Orderform Orderform) {
        OrderDTO orderDTO = new OrderDTO();
        Gson gson = new Gson();
        //DTO中填数据
        orderDTO.setBuyerName(Orderform.getName());
        orderDTO.setBuyerPhone(Orderform.getPhone());
        orderDTO.setBuyerAddress(Orderform.getAddress());
        orderDTO.setBuyerOpenid(Orderform.getOpenid());
        List<OrderDetail> orderDetailList = new ArrayList<>();
        try {
            //使用jON转换成List格式
            orderDetailList = gson.fromJson(Orderform.getItems(),
                    new TypeToken<List<OrderDetail>>() {
                    }.getType());
        } catch (Exception e) {
            log.error("【对象转换】错误, string={}", Orderform.getItems());
            throw new SellException(ResuletEnum.PARAM_ERROR);
        }
        orderDTO.setOrderMasterList(orderDetailList);
        return orderDTO;
    }
}
